<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>

    <table border="1">
        <thead style="background-color: pink;">
            <th><input type="checkbox" name="" id="btn1"></th>
            <th>商品</th>
            <th>价钱</th>
        </thead>
        <tbody>
            <tr>
                <td><input type="checkbox" name="" id="" class="itme"></td>
                <td>iPhone</td>
                <td>88888888</td>
            </tr>
            <tr>
                <td><input type="checkbox" name="" id="" class="itme"></td>
                <td>iPhone</td>
                <td>88888888</td>
            </tr>
            <tr>
                <td><input type="checkbox" name="" id="" class="itme"></td>
                <td>iPhone</td>
                <td>88888888</td>
            </tr>
        </tbody>
    </table>
    <script>
        //全选按钮的
        var btn1 = document.querySelector("#btn1")
        var btn = document.querySelectorAll(".itme")
        btn1.onclick = function () {
            for (var i = 0; i < btn.length; i++) {
                btn[i].checked = this.checked;
            }
        }
        //给下面的复选框绑定事件
        for (var i = 0; i < btn.length; i++) {
            btn[i].onclick = function () {
                //控制按钮是否被选中
                var flag=true;
                //每次点击都要检查所有复选框是否被选中
                for (var i = 0; i < btn.length; i++) {
                     if(!btn[i].checked){
                         flag=false;
                     }
                }
                btn1.checked=flag;
            }
        }
    </script>
</body>

</html>